package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.pojo.HomePageEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/9/5.
 */

public class ADA_NewsImgList extends RecyclerView.Adapter<ADA_NewsImgList.NewsImgViewHolder> {


    private Context mContext;
    private LayoutInflater mInflater;
    private List<HomePageEntity.ListBean.ImageListBean> mList;

    public ADA_NewsImgList(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void addList(List<HomePageEntity.ListBean.ImageListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public NewsImgViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NewsImgViewHolder(mInflater.inflate(R.layout.item_news_img_list, parent, false));
    }

    @Override
    public void onBindViewHolder(NewsImgViewHolder holder, int position) {
        HomePageEntity.ListBean.ImageListBean bean = mList.get(position);
        Glide.with(mContext)
                .load(bean.getImageUrl())
                .transform(new GlideRoundTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .placeholder(R.mipmap.ic_serve_default)
                .into(holder.ivImgNews);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class NewsImgViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img_news)
        ImageView ivImgNews;
        public NewsImgViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
